#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
#include "cmd.h"

void exec_cmd(struct cmd *cmd)
{
    if (cmd->input || cmd->output) {
        int input_fd = 0, output_fd = 1;
        if (cmd->input) {  
            input_fd = open(cmd->input, O_RDONLY);  
            if (input_fd == -1) { 
                exit(0);
            }
        dup2(input_fd, STDIN_FILENO);  
        close(input_fd);
        }

        if (cmd->output) { 
            output_fd = open(cmd->output, O_WRONLY | O_CREAT | O_TRUNC, 0644);
            if (output_fd == -1) {  
                exit(0);
            }
            dup2(output_fd, STDOUT_FILENO);  
            close(output_fd);
        }
    }
    if (cmd == NULL) {
         perror("Invalid command structure");
         exit(0);
    }
    if (execvp(cmd->argv[0], cmd->argv) == -1) {
         perror("execvp");
         exit(0);
    }
}

int builtin_cmd(struct cmd *cmd)
{
        if (strcmp(cmd->argv[0], "cd") == 0) {
            if (cmd->argc < 2) {
                printf("cd: missing operand\n");
                return 0;
            }
            if (chdir(cmd->argv[1]) == -1) {
                perror("chdir");                                                                return 0;
                                                                                            }                                                                               return 1;
                                                                                         } else if(strcmp(cmd->argv[0], "pwd") == 0) {
                                                                                            char cwd[1024];
                                                                                            if (getcwd(cwd, sizeof(cwd)) != NULL) {
                                                                                                 printf("%s\n", cwd);
                                                                                                 return 1;                                                                                                                                               } else {
                                                                                            perror("getcwd");                                                                                                                                               return 0;
                                                                                            }
                                                     
         } else if (strcmp(cmd->argv[0], "exit") == 0) {                                                                                                                   exit(0);                                                                        return 0;
         }                                                                                                                                                                                                                                               return 0;
}

